<?php

namespace application\member\controller;

use ticky\request;

class stat extends \application\common\controller\admin {

    private $title_stat = array('groupid' => '用户组', 'gender' => '性别', 'birthday' => '生日', 'constellation' => '星座', 'zodiac' => '生肖', 'idcardtype' => '证件类型', 'education' => '学历', 'bloodtype' => '血型',
        'birthyear' => '出生年份', 'birthmonth' => '出生月份', 'birthprovince' => '出生省份', 'birthcity' => '出生地', 'birthdist' => '出生县', 'birthcommunity' => '出生小区',
        'resideprovince' => '居住省份', 'residecity' => '居住地', 'residedist' => '居住县', 'residecommunity' => '居住小区'
    );

    /**
     * 资料统计项
     * */
    public function index() {
        if (empty($_GET['fieldid'])) {
            $fieldid = $_GET['fieldid'] = 'groupid';
        } else {
            $fieldid = $_GET['fieldid'];
        }

        if ($_GET['do'] == 'stepstat' && $_GET['t'] > 0 && $_GET['i'] > 0) {
            $t = intval($_GET['t']);
            $i = intval($_GET['i']);
            $o = $i - 1;
            $value = db('member_stat_field')->where(array('fieldid' => $fieldid))->limit($o . ',1')->select();
            if ($value) {
                $optionid = intval($value[0]['optionid']);
                $fieldvalue = $value[0]['fieldvalue'];
            } else {
                $optionid = 0;
                $fieldvalue = '';
            }
            $cnt = ($fieldid === 'groupid') ? db('member')->field(" COUNT(*) ")->where(array('groupid' => $fieldvalue))->one() :
                    db('member_profile')->field(" COUNT(*) ")->where(array($fieldid => $fieldvalue))->one();
            db('member_stat_field')->update(array('users' => $cnt, 'updatetime' => TIMESTAMP), array('optionid' => $optionid));
            if ($i < $t) {
                showmsg('更新统计数据中(' . $i . ' / ' . $t . ')，请稍候...', '/member/stat/index/?fieldid=' . $fieldid . '&do=stepstat&t=' . $t . '&i=' . ($i + 1));
            } else {
                showmsg('统计数据更新成功', '/member/stat/index/?fieldid=' . $fieldid);
            }
        }

        if (request::isPost()) {
            if ($_POST['optype'] == 'option') {//更新统计项
                $options = $inserts = $hits = $deletes = array();
                $items = db('member_stat_field')->where(array('fieldid' => $fieldid))->select();
                foreach ($items as $value) {
                    $options[$value['optionid']] = $value['fieldvalue'];
                    $hits[$value['optionid']] = false;
                }
                $alldata = $fieldid === 'groupid' ?
                        db('member')->field("DISTINCT(`groupid`)")->select() :
                        db('member_profile')->field("DISTINCT(`" . $fieldid . "`)")->select();
                foreach ($alldata as $value) {
                    $fieldvalue = $value[$fieldid];
                    $optionid = array_search($fieldvalue, $options);
                    if ($optionid) {
                        $hits[$optionid] = true;
                    } else {
                        $inserts[] = array('fieldid' => $fieldid, 'fieldvalue' => $fieldvalue);
                    }
                }
                foreach ($hits as $key => $value) {
                    if ($value == false) {
                        $deletes[] = $key;
                    }
                }
                if ($deletes) {
                    db('member_stat_field')->delete($deletes);
                }
                if ($inserts) {
                    foreach ($inserts as $value) {
                        $value['updatetime'] = NULL;
                        db('member_stat_field')->insert($value);
                    }
                }
                showmsg('更新统计项成功', '/member/stat/index/?fieldid=' . $fieldid);
            } elseif ($_POST['optype'] == 'data') {//更新数据
                $t = db('member_stat_field')->field(" COUNT(*) ")->where(array('fieldid' => $fieldid))->one();
                if ($t) {
                    showmsg('准备更新统计数据，共 ' . $t . ' 项。此过程将自动完成，请稍候...', '/member/stat/index/?fieldid=' . $fieldid . '&do=stepstat&t=' . $t . '&i=1');
                } else {
                    showmsg('统计数据更新成功', '/member/stat/index/?fieldid=' . $fieldid);
                }
            }
        } else {
            if ($fieldid) {
                $usergroups = db('member_group')->select();
                $items = db('member_stat_field')->where(array('fieldid' => $fieldid))->order('fieldvalue desc')->select();
                $total = 0;
                foreach ($items as $row) {
                    $total += $row['users'];
                }
                for ($i = 0, $L = count($items); $i < $L; $i++) {
                    if ($fieldid == 'groupid') {
                        $items[$i]['fieldvalue'] = $usergroups[$items[$i]['fieldvalue']]['name'];
                    } elseif ($fieldid == 'gender') {
                        $items[$i]['fieldvalue'] = L('gender_' . $items[$i]['fieldvalue']);
                    } elseif (empty($items[$i]['fieldvalue'])) {
                        $items[$i]['fieldvalue'] = '未填写';
                    }
                    $items[$i]['updatetime'] = !empty($items[$i]['updatetime']) ? $items[$i]['updatetime'] : 'N/A';
                    if ($total) {
                        $items[$i]['percent'] = intval(10000 * $list[$i]['users'] / $total) / 100;
                    } else {
                        $items[$i]['percent'] = 0;
                    }
                    $items[$i]['width'] = $items[$i]['percent'] ? intval($items[$i]['percent'] * 2) : 1;
                }
            }

            $this->assign('total', $total);
            $this->assign('items', $items);
            $this->assign('title_stat', $this->title_stat);
            $this->display('member/member_stat');
        }
    }

}
